// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.wizards;

import java.util.List;

import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.core.resource.WarehouseResourceFactory;
import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.designer.ui.DesignerUIPlugin;
import org.dengues.model.reports.Reports;
import org.dengues.reports.i18n.Messages;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2008-1-7 qiang.zhang $
 * 
 */
public class NewReportWizardPage extends WizardPage {

    private String name;

    private String comment;

    private IStatus nameStatus;

    private Reports reports;

    private IPath path;

    private boolean isActived;

    /**
     * Qiang.Zhang.Adolf@gmail.com NewReportWizardPage constructor comment.
     * 
     * @param destinationPath
     */
    public NewReportWizardPage(IPath destinationPath) {
        this();
        this.path = destinationPath;
    }

    private NewReportWizardPage() {
        super(""); //$NON-NLS-1$
        nameStatus = createOkStatus();
        setTitle(Messages.getString("NewReportWizard.createTitle")); //$NON-NLS-1$
        setDescription(Messages.getString("NewReportWizardPage.createDesc")); //$NON-NLS-1$
    }

    public NewReportWizardPage(Reports process, boolean isActived) {
        super(""); //$NON-NLS-1$
        nameStatus = createOkStatus();
        setTitle(Messages.getString("NewReportWizard.editTitle")); //$NON-NLS-1$
        setDescription(Messages.getString("NewReportWizardPage.editDesc")); //$NON-NLS-1$
        this.reports = process;
        this.isActived = isActived;
    }

    private Text nameText;

    private Text commentText;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout(2, false);
        container.setLayout(layout);
        Label nameLab = new Label(container, SWT.NONE);
        nameLab.setText(Messages.getString("NewReportWizardPage.label.name")); //$NON-NLS-1$

        nameText = new Text(container, SWT.BORDER);
        nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        if (reports != null) {
            name = DenguesTextUtil.getNotNullString(reports.getName());
            nameText.setText(name);
        }

        Label commentLab = new Label(container, SWT.NONE);
        commentLab.setText(Messages.getString("NewReportWizardPage.label.comment")); //$NON-NLS-1$

        commentText = new Text(container, SWT.BORDER | SWT.MULTI);
        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
        gridData.heightHint = 60;
        commentText.setLayoutData(gridData);
        if (reports != null) {
            comment = DenguesTextUtil.getNotNullString(reports.getComment());
            commentText.setText(comment);
        }

        setControl(container);
        setPageComplete(false);
        addListeners();
        checkFieldsValue();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "checkFieldsValue".
     */
    private void checkFieldsValue() {
        if (isActived) {
            nameText.setEnabled(false);
            commentText.setEnabled(false);
            setPageComplete(false);
            setErrorMessage(Messages.getString("NewReportWizardPage.activeMsg")); //$NON-NLS-1$
        } else {
            if (nameText.getText().length() == 0) {
                nameStatus = new Status(IStatus.ERROR, DesignerUIPlugin.PLUGIN_ID, IStatus.ERROR, Messages
                        .getString("NewReportWizardPage.isEmpty"), //$NON-NLS-1$
                        null);
            } else {
                if (isProcessNameAlreadyUsed(nameText.getText())) {
                    nameStatus = new Status(IStatus.ERROR, DesignerUIPlugin.PLUGIN_ID, IStatus.ERROR, Messages
                            .getString("NewReportWizardPage.isExist"), null); //$NON-NLS-1$
                } else {
                    nameStatus = createOkStatus();
                }
            }
            setMessage(nameStatus);
            boolean b = nameStatus.getSeverity() != IStatus.ERROR;
            setPageComplete(b);
            if (b) {
                name = nameText.getText();
            }
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "isProcessNameAlreadyUsed".
     * 
     * @param text
     * @return
     */
    private boolean isProcessNameAlreadyUsed(String text) {
        List<String> names = WarehouseResourceFactory.getAllObjectNamesFromEMF(ENodeCategoryName.REPORTS);
        if (reports != null && reports.getName().equals(text)) {
            return false;
        }
        return names.contains(text);
    }

    /**
     * zx Comment method "addListeners".
     */
    private void addListeners() {
        nameText.addModifyListener(new ModifyListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent e) {
                checkFieldsValue();
            }

        });
        commentText.addModifyListener(new ModifyListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent e) {
                comment = commentText.getText();
            }
        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getModelComment".
     * 
     * @return
     */
    public String getModelComment() {
        return this.comment;
    }

    public String getModelName() {
        return this.name;
    }

    public String getModelStatus() {
        return ENodeStatus.NORMAL.getId();
    }

    private void setMessage(IStatus status) {
        if (IStatus.ERROR == status.getSeverity()) {
            setErrorMessage(status.getMessage());
            setMessage(""); //$NON-NLS-1$
        } else {
            setMessage(status.getMessage());
            setErrorMessage(null);
        }
    }

    private static IStatus createOkStatus() {
        return new Status(IStatus.OK, DesignerUIPlugin.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
    }
}
